﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class SendMessageDAO : BaseService
    {

        public IList<SendMessage> GetListPaging(int page, int pagesize, int userid, int state,bool isDel,bool isPub,DateTime date1,DateTime date2,string key)
        {
            string hql = "from SendMessage where 1=1";
            if (!string.IsNullOrEmpty(key))
            {
                hql += " and UserName like '%," + key + ",%' or ReciveWorkerName like '%," + key + ",%'";
            }
            if (date1 > Convert.ToDateTime("1970-1-1"))
            {
                hql += " and AddDate >'" + date1+"'";
            }
            if (date2 > Convert.ToDateTime("1970-1-1"))
            {
                hql += " and AddDate <'" + date2+"'";
            }
            if (state != -99)
            {
                hql += " and State =" + state;
            }
            hql += " and IsUser ="+userid+" and IsDel ="+isDel+" and IsPub = "+isPub+" order By AddDate Desc";
            return FindList<SendMessage>(hql, null, (page - 1) * pagesize, pagesize);
        }

        public int GetListPaging(int userid, int state, bool isDel, bool isPub, DateTime date1, DateTime date2, string key)
        {
            string hql = "select count(*) from SendMessage where 1=1";
            if (!string.IsNullOrEmpty(key))
            {
                hql += " and UserName like '%" + key + "%' or ReciveWorkerName like ',%" + key + ",%'";
            }
            if (date1 > Convert.ToDateTime("1970-1-1"))
            {
                hql += " and AddDate >'" + date1 + "'";
            }
            if (date2 > Convert.ToDateTime("1970-1-1"))
            {
                hql += " and AddDate <'" + date2 + "'";
            }
            if (state != -99)
            {
                hql += " and State =" + state;
            }
            hql += " and IsUser =" + userid + " and IsDel =" + isDel + " and IsPub = " + isPub ;
            try
            {

           
            return Convert.ToInt32(GetValue(hql));
            }
            catch (Exception)
            {

                return 0;
            }
        }
        /// <summary>
        /// 查询待完成信件
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isPub"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<SendMessage> GetListPaging(int userid,bool isPub,bool isDel,int page,int pagesize)
        {
            string hql = "from SendMessage where 1=1 and IsUser=" + userid + " and IsPub=" + isPub + " and IsDel = " + isDel + " order by AddDate desc";
            try
            {
                return FindList<SendMessage>(hql, null, (page - 1) * pagesize, pagesize);
            }
            catch (Exception)
            {
                
                return null;
            }
            
        }
        /// <summary>
        /// 查询删除信件
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<SendMessage> GetListPaging(int userid, bool isDel, int page, int pagesize)
        {
            string hql = "from SendMessage where 1=1 and IsUser=" + userid + " and  IsDel = " + isDel + " order by AddDate desc";
            try
            {
                return FindList<SendMessage>(hql, null, (page - 1) * pagesize, pagesize);
            }
            catch (Exception)
            {
                return null;

            }
        }

        /// <summary>
        /// 查询删除信件数量
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isPub"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public int GetListPaging(int userid, bool isDel)
        {
            string hql = "select count(*) from SendMessage where 1=1 and IsUser=" + userid + " and  IsDel = " + isDel;
            try
            {
                return Convert.ToInt32(GetValue(hql));
            }
            catch (Exception)
            {

                return 0;
            }
        }


        /// <summary>
        /// 查询待完成信件数量
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isPub"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public int  GetListPaging(int userid, bool isPub, bool isDel)
        {
            string hql = "select count(*) from SendMessage where 1=1 and IsUser=" + userid + " and IsPub=" + isPub + " and IsDel = " + isDel;

            try
            {
                return Convert.ToInt32(GetValue(hql));
            }
            catch (Exception)
            {
                
                return 0;
            }
        }

        /// <summary>
        /// 批量修改暂存
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchSendMessageUpdate(string ids)
        {
            string hql = "update SendMessage set IsPub = false where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 批量修改删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchSendMessageDelete(string ids)
        {
            string hql = "update SendMessage set IsDel = true where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 批量修改删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchSSendMessageUpdate(string ids)
        {
            string hql = "update SendMessage set IsDel= false where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchSSendMessageDelete(string ids)
        {
            string hql = "delete SendMessage where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }



    }
}
